using System;
using System.IO;
using System.Reflection;
using ExamServer.Service;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Microsoft.EntityFrameworkCore;
using AutoMapper;

namespace ExamServer
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        //依赖注入:DI
        public void ConfigureServices(IServiceCollection services)
        {
            //依賴注入示例
            //services.AddSingleton<IMyDependency, MyDependency>(); //將IMyDependency依賴注入 Singleton 单例 服务从开启到结束，只创建一次的实例
            // services.AddScoped 范围内的，从客户端连接到服务上到客户端关闭，创建一次实例
            //services.AddTransient 暂时的，每次请求都会创建一次实例
            //注入跨域
            services.AddCors(options =>
            {
                options.AddPolicy("any", builder =>
          {
                  builder.AllowAnyMethod()
                    .SetIsOriginAllowed(_ => true)//允许任何来源的主机访问
                    .AllowAnyHeader()
                    .AllowCredentials();//指定处理cookie
        });
            });
            //注入控制器
            services.AddControllers();
            //添加Swagger的服务
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title = "ExamServer API",
                    Version = "V1",
                    Description = "ExamServer項目的API文档"
                });
                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                options.IncludeXmlComments(xmlPath);
            });
            //注入数据库操作上下文
            services.AddDbContext<ExamServerContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("ExamServerConnection"));
            });
            //注入实体类操作接口与实现类的注入
            services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();
            //注入AutoMapper服务
            services.AddAutoMapper(typeof(Startup));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger) //添加日志记录
        {
            if (env.IsDevelopment())
            {
                logger.LogInformation("In Development environment"); //日志输出
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            //使用Swagger中间件
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json", "ExamServer API V1");
            });

            app.UseRouting();
            app.UseCors("any");
            app.UseAuthorization();

            //中间件
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

        }
    }
}
